package pl.msikora.android.lapmagic.view;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockListActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import pl.msikora.android.lapmagic.R;
import pl.msikora.android.lapmagic.database.LapMagicDatabaseHelper;
import pl.msikora.android.lapmagic.database.TracksTable;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.support.v4.widget.SimpleCursorAdapter;

/**
 * View which represents interactive list of existing tracks.
 * 
 * @author Maciej Sikora
 * 
 */
public class TracksActivity extends SherlockListActivity {
	
	private final String TAG = "TracksActivity";
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.clickable_list);
		TextView textOnEmpty = (TextView) findViewById(android.R.id.empty);
		textOnEmpty.setText(R.string.tracks_no_tracks);
		ActionBar actionBar = getSupportActionBar();
		actionBar.setHomeButtonEnabled(true);
		actionBar.setTitle(R.string.tracks_title);
	}
	
	@Override
	public void onStart() {
		super.onStart();
		Log.d(TAG, "Filling the list view with data during start");
		fillData();
	}
	
	@Override
	public void onDestroy() {
		Log.d(TAG, "Closing database on destroy");
		LapMagicDatabaseHelper.getInstance(this).close();
		super.onDestroy();
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.tracks_menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
    	case R.id.newTrack:
    		Intent intent = new Intent(this, TrackDetailActivity.class);
    		startActivity(intent);
    		return true;
    	case android.R.id.home:
    		Intent home = new Intent(this, MainActivity.class);
    		home.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    		startActivity(home);
    		return true;
    	}
    	return super.onOptionsItemSelected(item);
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	super.onListItemClick(l, v, position, id);
    	Intent intent = new Intent(this, TrackDetailActivity.class);
    	Log.d(TAG, "list item clicked, track id = " + id);
    	intent.putExtra("id", id);
    	startActivity(intent);
    }
	
    private void fillData() {
		SQLiteDatabase db = LapMagicDatabaseHelper.getInstance(this)
				.getReadableDatabase();
		Cursor tracksCursor = db.query(TracksTable.TABLE_TRACKS, new String[] {
				TracksTable.COLUMN_ID, TracksTable.COLUMN_NAME}, null, 
				null, null, null, null);
		startManagingCursor(tracksCursor);
		
		String[] from = new String[] { TracksTable.COLUMN_NAME };
		int[] to = new int[] { R.id.trackLabel };
		SimpleCursorAdapter tracks = 
				new SimpleCursorAdapter(this, R.layout.tracks_row, tracksCursor, 
						from, to, 0);
		setListAdapter(tracks);
	}
    
}
